/*
 * Copyright (c) 2016-20017 Max Cong <savagecm@qq.com>
 * this code can be found at https://github.com/maxcong001/connection_manager
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
#include <unordered_set>
#include <queue>
#include <memory>
#include <unordered_map>
#include <list>
#include <tuple>
#include <atomic>
#include <thread>
#include "logger/logger.hpp"
#include "connManager/messagebus.h"

// this is for messagebus for conn
#define REM_CONN "rem_conn"

// this is for messagebus for pool
#define CONN_INC "conn_inc"
#define CONN_DEC "conn_dec"
// this is for messagebus for manager
#define POOL_INC "pool_inc"
#define POOL_DEC "pool_dec"

struct ConnInfo
{
    std::string sourceIP;
    std::string sourcePort;
    std::string destIP;
    std::string destPort;
    int type; // connection type(now we have 0 local, others remote)

    ConnInfo &operator=(const ConnInfo &info)
    {
        this->sourceIP = info.sourceIP;
        this->sourcePort = info.sourcePort;
        this->destIP = info.destIP;
        this->destPort = info.destPort;
        this->type = info.type;
        return *this;
    }

    bool operator==(const ConnInfo &rhs)
    {
        return !((this->sourceIP).compare(rhs.sourceIP)) && !((this->sourcePort).compare(rhs.sourcePort)) && !((this->destIP).compare(rhs.destIP)) && !((this->destPort).compare(rhs.destPort)) && this->type == rhs.type;
    }

    std::string toString()
    {
        return sourceIP + sourcePort + destIP + destPort + std::to_string(type);
    }

    bool operator<(const ConnInfo &r) const
    {

        return std::tie(r.sourceIP, r.sourcePort, r.destIP, r.destPort, r.type) < std::tie(sourceIP, sourcePort, destIP, destPort, type);
    }

    void dump()
    {
        __LOG(debug, "conn info ： \n"
                         << "type is : " << ((!type) ? "local" : "remote") << " dest IP is : " << destIP << " dest port is : " << destPort);
    }
};
